use chrono::{DateTime, Local, LocalResult, NaiveDateTime, TimeZone, Utc};
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;

#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, Default)]
pub struct AcousticFileInfo {
    pub file_name: String,
    pub file_path: String,
    pub start_time: DateTime<Utc>,
    pub end_time: DateTime<Utc>,
    pub ip_identifier: String,
    pub ip_address: String,
    pub channels: usize,
    pub sampling_rate: usize,
    pub bit_depth: usize,
    pub voltage_range: Option<f32>,
    pub file_size: u64,
    pub duration: f64,
}

impl AcousticFileInfo {
    pub fn parse_time(time_str: &str) -> Option<DateTime<Utc>> {
        let naive = NaiveDateTime::parse_from_str(time_str, "%Y-%m-%d %H-%M-%S").ok()?;
        let utc_naive = naive.and_utc();
        let local_dt = match Local.from_local_datetime(&naive) {
            LocalResult::Single(dt) => dt,
            LocalResult::Ambiguous(dt, _) => dt,
            LocalResult::None => Local
                .timestamp_opt(utc_naive.timestamp(), utc_naive.timestamp_subsec_nanos())
                .single()?,
        };
        Some(local_dt.with_timezone(&Utc))
    }
}
